import { Breadcrumb, message } from 'antd';
import React, { FC } from 'react';
import { Task } from 'lfun-bg-comp';
import _ from 'lodash';
import { PageContainer } from '@/component/page-container';
import { stopTask } from '@/service/teacher/task';
import { get_stoge_pz } from '@/utils';

const _list = [
  {
    taskNo: 'policytask1',
    taskStatus: 4,
    app: '基础教育在线布置作业',
    chunk: '应用策略清单',
    createDate: '2024-01-22 13:27:02',
    operate: '运营A',
    progress: '70',
    uploadTotal: '200',
    failUploadTotal: '140',
  },
  {
    taskNo: 'policytask2',
    taskStatus: 1,
    app: '基础教育在线布置作业',
    chunk: '应用策略清单',
    createDate: '2024-01-22 13:27:02',
    operate: '运营A',
    progress: '100',
    uploadTotal: '200',
    failUploadTotal: '200',
  },
  {
    taskNo: 'policytask3',
    taskStatus: 2,
    app: '基础教育在线布置作业',
    chunk: '应用策略清单',
    createDate: '2024-01-22 13:27:02',
    operate: '运营A',
    failReson: '导入失败，无法读取到模板中数据',
  },
  {
    taskNo: 'policytask4',
    taskStatus: 3,
    app: '基础教育在线布置作业',
    chunk: '应用策略清单',
    createDate: '2024-01-22 13:27:02',
    operate: '运营A',
    progress: '50',
    uploadTotal: '200',
    failUploadTotal: '200',
  },
  {
    taskNo: 'policytask5',
    taskStatus: 1,
    app: '基础教育在线布置作业',
    chunk: '应用策略清单',
    createDate: '2024-01-22 13:27:02',
    operate: '运营A',
    progress: '100',
    uploadTotal: '200',
    failUploadTotal: '20',
  },
];
const Groups: FC = () => {
  const [pageIndex, setPageIndex] = React.useState(1);
  const [pageSize, setPageSize] = React.useState(get_stoge_pz(5));
  const [list, setList] = React.useState([]);
  const search = (searchParams, pageIdx, pageSize) => {
    setPageIndex(pageIdx);
    setPageSize(pageSize);
  };

  return (
    <PageContainer header={<Breadcrumb items={[{ title: '任务管理' }]} />}>
      <Task
        getTaskSrv="/school/v1/task/page"
        list={list}
        listCount={100}
        pageIndex={pageIndex}
        pageSize={pageSize}
        onSearch={search}
        onOperate={(item: any, research) => {
          try {
            if (item.taskStatus === 'pending' || item.taskStatus === 'running') {
              stopTask(item.task_id).then(res => {
                if (res.success) {
                  research();
                } else {
                  message.error(res.message || res.details);
                }
              });
              return;
            }
            if (['completed', 'succeeded'].indexOf(item.taskStatus) !== -1) {
              window.location.href = item.download_task_result_url;
            } else {
              window.location.href = item.download_task_source_file_url;
            }
          } catch (e) {
            console.log(e);
          }
        }}
      />
    </PageContainer>
  );
};

export default Groups;
